package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.frame.BaseNetAns;
import com.cn.goodsadmin.common.frame.NetService;
import com.cn.goodsadmin.common.frame.RequestInfo;
import com.cn.goodsadmin.config.BaseConfig;
import com.cn.goodsadmin.config.CgiConfig;
import com.cn.goodsadmin.po.Words;
import com.cn.goodsadmin.requestinfo.GetAcessTokenAns;
import com.cn.goodsadmin.requestinfo.WxSendMsgRequestInfo;
import org.springframework.stereotype.Service;

@Service("WxService")
public class WxService extends BaseService{

    public boolean wxSendMsg(String message,String touser,String wxNumber,String appid,String msgid)throws Exception{
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.url = BaseConfig.getacessUrl;
        requestInfo.appId = appid;
        requestInfo.requestKey = CgiConfig.GETACESSTOKEN;
        GetAcessTokenAns acessTokenAns = (GetAcessTokenAns) NetService.getInstance().startSynHttpNetWork(requestInfo);
        if(!acessTokenAns.isError){
            String aceessthoken = acessTokenAns.aceesToken;
            WxSendMsgRequestInfo wxSendMsgRequestInfo=new WxSendMsgRequestInfo();
            wxSendMsgRequestInfo.url=BaseConfig.WXSENDMSGURL+aceessthoken;
            wxSendMsgRequestInfo.message=message;
            wxSendMsgRequestInfo.touser=touser;
            wxSendMsgRequestInfo.requestKey=CgiConfig.WXSENDMESSAGE;
            BaseNetAns ans= NetService.getInstance().startSynHttpNetWork(wxSendMsgRequestInfo);
            if(!ans.isError){
                //回复入库
                Words words=new Words();
                words.setcContent(message);
                words.setcFromusername(wxNumber);
                words.setcMsgId(msgid);
                words.setcTousername(touser);
                words.setcMsgtype(wxSendMsgRequestInfo.msgType);
                wordsDao.save(words);
            }
            return ans.isError;
        }
        return true;
    }
}
